﻿

module TestCancelToken 

open System
open System.Threading
open System.Threading.Tasks
open TestHelpers

// CTS can be only canceled once
// after canceled, all the async job started with this CTS.token
// won't even be scheduled
let private test_CTS_NotReusable() =

    let job = async {
        while true do
            printf "*"
            do! Async.Sleep 100
    }

    let cts = new CancellationTokenSource()
    Async.StartImmediate(job,cts.Token)

    pause()
    cts.Cancel()

    pause()
    printfn "start async job with a canceled CTS"
    // the result is: this time, the async job won't even start
    // because the CTS is already canceled, the async job won't even be scheduled
    Async.StartImmediate(job, cts.Token)

    pause()

let testmain() =
    test_CTS_NotReusable()



